package org.ubc.cs410.disposal2020;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/*
 * Methods related to the game
 */

public class GameFactory {
	public static Game getCurrentGame() throws NoActiveGameException {
		Player player = PlayerFactory.getCurrentPlayer();
		if (player == null) {
			throw new NoActiveGameException();
		} else {
			return player.getGame();
		}
	}

	public static Game getGameBySlug(String slug) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Game game = getGameBySlug(slug, pm);
		pm.close();
		return game;
	}

	public static Game getGameBySlug(String slug, PersistenceManager pm) {
		Query query = pm.newQuery(Game.class);
		query.setFilter("slug == gameSlugParam");
		query.declareParameters("String gameSlugParam");

		List<Game> results = (List<Game>) query.execute(slug);
		if (!results.isEmpty()) {
			return results.get(0);
		} else {
			return null;
		}
	}
}
